package com.thssh.ui.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.thssh.ui.R;

/**
 * @author zhangyugehu
 * @version V1.0
 * @data 2017/09/07
 */

public class MoreItemView extends FrameLayout {

    /** 左右图标是否可见 */
    private boolean isLeftVisibility, isRightVisibility;
    private String mTitle, mSubTitle;
    /** 左右图标资源文件 */
    private int mLeftSrcRid, mRightSrcRid;

    private ImageView mLeftView, mRightView;
    private TextView mTitleView, mSubTitleView;

    public MoreItemView(Context context) {
        this(context, null);
    }

    public MoreItemView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MoreItemView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        prepareAttributeSet(context, attrs);
        inflaterView(context);
        handlerData();
    }

    private void handlerData() {
        if(mLeftSrcRid > 0) { mLeftView.setImageResource(mLeftSrcRid); }
        if(mTitle != null) { mTitleView.setText(mTitle); }
        if(mSubTitle != null) { mSubTitleView.setText(mSubTitle); }
        if(mLeftSrcRid > 0) { mRightView.setImageResource(mRightSrcRid); }
        mLeftView.setVisibility(isLeftVisibility ? View.VISIBLE : View.GONE);
        mRightView.setVisibility(isRightVisibility ? View.VISIBLE : View.GONE);
    }

    private void prepareAttributeSet(Context context, AttributeSet attrs) {
        // 获取自定义属性
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MoreItemView);
        mTitle = typedArray.getString(R.styleable.MoreItemView_title);
        mSubTitle = typedArray.getString(R.styleable.MoreItemView_subTitle);
        mLeftSrcRid = typedArray.getResourceId(R.styleable.MoreItemView_leftSrc, R.drawable.ic_launcher_background);
        mRightSrcRid = typedArray.getResourceId(R.styleable.MoreItemView_rightSrc, R.drawable.ic_launcher_background);
        isLeftVisibility = typedArray.getBoolean(R.styleable.MoreItemView_leftVisibility, false);
        isRightVisibility = typedArray.getBoolean(R.styleable.MoreItemView_rightVisibility, true);

        // 回收资源
        typedArray.recycle();
    }

    private void inflaterView(Context context) {
        // 将布局加载出来并填充到当前ViewGroup
        LayoutInflater.from(context).inflate(R.layout.view_more_item, this, true);
        mTitleView = findViewById(R.id.item_title);
        mSubTitleView = findViewById(R.id.item_sub_title);
        mLeftView = findViewById(R.id.item_left_icon);
        mRightView = findViewById(R.id.item_right_icon);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }
}
